﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using Newtonsoft.Json;
using UFIDA.U8.BJSOFT.LF.MES.WEBAPI.DBHelp;
using UFIDA.U8.BJSOFT.LF.MES.WEBAPI.Models;

namespace UFIDA.U8.BJSOFT.LF.MES.WEBAPI.Method
{
	public class AddRD11
	{
		public static string Insert(string filename, string jsons)
		{
			LogException.WriteiLog(filename, "接收到的json:" + jsons, LogException.ilog, false);
			Result mr = new Result();
			List<ItemsItemR> lim = new List<ItemsItemR>();
			
			int row = 0;
			string res = "";
			bool flag = !string.IsNullOrEmpty(jsons);
			if (flag)
			{
				try
				{
					List<string> list = new List<string>();
					RD10 rd = JsonConvert.DeserializeObject<RD10>(jsons);
					string cBusType = rd.cBusType;
					bool flag2 = string.IsNullOrEmpty(cBusType);
					if (flag2)
					{
						row = 1;
						res += "业务类型不允许为空!检查cBusType字段";
					}
					bool flag3 = cBusType != "生产" && cBusType != "委外";
					if (flag3)
					{
						row = 1;
						res += "业务类型传值不正确!检查cBusType字段;可填值为[生产|委外]";
					}
					string dCode = rd.dCode;
					bool flag4 = string.IsNullOrEmpty(dCode);
					if (flag4)
					{
						row = 1;
						res += "外部系统单号不允许为空!检查dCode字段";
					}
					else
					{
						int ifex = Convert.ToInt32(SQLHelper.GetString(string.Concat(new string[]
						{
							"select COUNT(*) from ",
							AddRD11.DBname,
							"..rdrecord11 where cCode='",
							dCode,
							"'"
						})));
						bool flag5 = ifex > 0;
						if (flag5)
						{
							row = 1;
							res = string.Concat(new string[]
							{
								res,
								"外部系统单号[",
								dCode,
								"]在U8系统已存在",
								filename,
								"信息!"
							});
						}
					}
					string dDate = rd.dDate;
					bool flag6 = !string.IsNullOrEmpty(dDate);
					if (flag6)
					{
						bool flag7 = !PublicMet.IsDate(dDate);
						if (flag7)
						{
							row = 1;
							res += "日期格式不正确!例：[2024-11-12]";
						}
					}
					else
					{
						row = 1;
						res += "日期不允许为空！检查dDate字段";
					}
					string RedVouch = rd.RedVouch;
					bool flag8 = RedVouch != "1" && RedVouch != "0";
					if (flag8)
					{
						row = 1;
						res += "红蓝单据类型非法;检查RedVouch字段。[1：红字0：蓝字]";
					}
					string cRdCode = rd.cRdCode;
					bool flag9 = string.IsNullOrEmpty(cRdCode);
					if (flag9)
					{
						row = 1;
						res += "类型不允许为空!检查cRdCode字段";
					}
					else
					{
						int ifex2 = Convert.ToInt32(SQLHelper.GetString(string.Concat(new string[]
						{
							"select COUNT(*) from ",
							AddRD11.DBname,
							"..Rd_Style where cRdCode='",
							cRdCode,
							"'"
						})));
						bool flag10 = ifex2 == 0;
						if (flag10)
						{
							row = 1;
							res = res + "类型[" + cRdCode + "]在U8系统不存在!";
						}
					}
					string cWhCode = rd.cWhCode;
					bool flag11 = string.IsNullOrEmpty(cWhCode);
					if (flag11)
					{
						row = 1;
						res += "仓库编码不允许为空!检查cWhCode字段";
					}
					else
					{
						int ifex3 = Convert.ToInt32(SQLHelper.GetString(string.Concat(new string[]
						{
							"select COUNT(*) from ",
							AddRD11.DBname,
							"..Warehouse where cWhCode='",
							cWhCode,
							"'"
						})));
						if (ifex3==0)
						{
							row = 1;
							res = res + "仓库编码[" + cWhCode + "]在U8系统不存在!";
						}
					}
					string cMaker = rd.cMaker;
					bool flag12 = string.IsNullOrEmpty(cMaker);
					if (flag12)
					{
						row = 1;
						res += "制单人不允许为空!检查cMaker字段";
					}
					else
					{
						string cMakers = PublicMet.CUSERNAME(cMaker);
						if (string.IsNullOrEmpty(cMakers))
						{
							row = 1;
							res += "未找到编码为[" + cMaker + "]的制单人员!";
						}
						else
						{
							cMaker = cMakers;
						}
					}
					string cHandler = rd.cHandler;
					bool flag13 = string.IsNullOrEmpty(cHandler);
					if (flag13)
					{
						row = 1;
						res += "审核人不允许为空!检查cHandler字段";
					}
					else
					{
						string cHandlers = PublicMet.CUSERNAME(cHandler);
						if (string.IsNullOrEmpty(cHandlers))
						{
							row = 1;
							res += "未找到编码为[" + cHandler + "]的审核人员!";
						}
						else
						{
							cHandler = cHandlers;
						}
					}
					string cDepCode = rd.cDepCode;
					bool flag15 = !string.IsNullOrEmpty(cDepCode);
					if (flag15)
					{
						int ifex4 = Convert.ToInt32(SQLHelper.GetString(string.Concat(new string[]
						{
							"select COUNT(*) from ",
							AddRD11.DBname,
							"..Department where cDepCode='",
							cDepCode,
							"'"
						})));
						bool flag16 = ifex4 == 0;
						if (flag16)
						{
							row = 1;
							res = res + "部门编码[" + cDepCode + "]在U8系统不存在!";
						}
					}
					else
					{
						row = 1;
						res = res + "部门编码不允许为空，检查cDepCode字段!";
					}
					string cMemo = rd.cMemo;
					string CBUTYPE = "领料";
					string cSource = "生产订单";
					string cAcc_Id = AddRD11.DBname.Substring(7, 3);
					int fidrd = 0;
					int cidrd = 0;
					string sql = "select iFatherId,iChildId  from UFSystem..UA_Identity where cAcc_Id='" + cAcc_Id + "' and cVouchType='rd' ";
					DataTable dtidrd = SQLHelper.GetDataTable(sql);
					bool flag17 = dtidrd.Rows.Count > 0;
					if (flag17)
					{
						fidrd = Convert.ToInt32(dtidrd.Rows[0]["iFatherId"].ToString());
						cidrd = Convert.ToInt32(dtidrd.Rows[0]["iChildId"].ToString());
					}
					else
					{
						sql = " insert into UFSystem..UA_Identity (cAcc_Id,cVouchType,iFatherId,iChildId)  values('" + cAcc_Id + "','rd',0,0) ";
						SQLHelper.ExecuteSql(sql);
					}
					string ID10 = "";
					bool flag18 = cBusType == "委外";
					if (flag18)
					{
						fidrd++;
						SQLHelper.ExecuteSql("update UFSystem..UA_Identity set iFatherId=iFatherId+1 where cAcc_Id='" + cAcc_Id + "' and cVouchType='rd' ");
						ID10 = "1" + string.Format("{0:D9}", fidrd);
						string Code10 = dCode;
						string csysbarcode = "||st10|" + Code10;
						string VT_ID = "63";
						sql = string.Concat(new string[]
						{
							"insert into ",
							AddRD11.DBname,
							"..rdrecord10(id,brdflag,cvouchtype,cbustype,cRdcode,cdepcode,csource,cwhcode,ddate,ccode,cmaker,bpufirst," +
							" biafirst,vt_id,bisstqc,iproorderid,iswfcontrolled,dnmaketime,dnmodifytime,dnverifytime,iprintcount,cHandler,dVeriDate  )" +
							" values (N'",ID10,"',N'1',N'10',N'成品入库','102','",cDepCode,"',N'库存',N'",cWhCode,"',N'",dDate,"',N'",dCode,"',N'",cMaker,"',0," +
							" 0,",VT_ID,",0,N'0',0, getdate(), Null , getdate() ,0,'"+cHandler+"','"+dDate+"')"
						});
						LogException.WriteiLog(filename, "RD10主表新增语句:" + sql, LogException.ilog, false);
						list.Add(sql);
						CBUTYPE = "委外发料";
						cSource = "委外订单";
					}
					fidrd++;
					SQLHelper.ExecuteSql("update UFSystem..UA_Identity set iFatherId=iFatherId+1 where cAcc_Id='" + cAcc_Id + "' and cVouchType='rd' ");
					string ID11 = "1" + string.Format("{0:D9}", fidrd);
					string Code11 = dCode;
					string csysbarcode2 = "||st11|" + Code11;
					string VT_ID2 = "65";
					sql = string.Concat(new string[]
					{
						"insert into ",
						AddRD11.DBname,
						"..rdrecord11(ID,bRdFlag,cVouchType,cBusType,cSource,cWhCode,dDate,cCode,cRdCode,cDepCode, " +
						" cHandler,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,iMQuantity,VT_ID,bIsSTQc,cPsPcode, " +
						" cMPoCode,iproorderid,bOMFirst,bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,iverifystate,iswfcontrolled,dnmaketime, " +
						" dnverifytime,bredvouch,bmotran,bHYVouch,iPrintCount,csysbarcode,cdefine10,cmemo,cvencode) " +
						" values('",ID11,"','0','11','",CBUTYPE,"','",cSource,"','",cWhCode,"','",dDate,"','",Code11,"','",cRdCode,"','",cDepCode,"','",
						cHandler,"',0,'",cMaker,"',convert(varchar(10),getdate(),23),0,0,'",0.ToString(),"','",VT_ID2,"',0,NULL," +
						" NULL,NULL,0,0,0,0,0,0,0,getdate(),getdate(),0,0,0,0,'",csysbarcode2,"',NULL,'",cMemo,"',NULL)"
					});
					list.Add(sql);
					int cou = rd.Items.Count;
					for (int i = 0; i < cou; i++)
					{
						ItemsItemR mir = new ItemsItemR();
						string mocode = "";
						string omcode = "";
						string moid = "";
						string modids = "";
						string imoseq = "";
						string cvencode = "";
						decimal iProcessFee = 0m;
						string Autoid = rd.Items[i].Autoid;
						bool flag19 = string.IsNullOrEmpty(Autoid);
						if (flag19)
						{
							row = 1;
							res = res + "第[" + (i + 1).ToString() + "]行U8工单明细唯一标识ID不允许为空!检查Autoid字段";
						}
						else
						{
							bool flag20 = cBusType == "生产";
							if (flag20)
							{
								int ifex5 = Convert.ToInt32(SQLHelper.GetString(string.Concat(new string[]
								{
									"select COUNT(*) from ",
									AddRD11.DBname,
									"..mom_orderdetail where modid='",
									Autoid,
									"'"
								})));
								bool flag21 = ifex5 == 0;
								if (flag21)
								{
									row = 1;
									res = string.Concat(new string[]
									{
										res,
										"第[",
										(i + 1).ToString(),
										"]行U8工单明细唯一ID[",
										Autoid,
										"]在U8系统不存在!"
									});
								}
								else
								{
									DataTable dtm = SQLHelper.GetDataTable(string.Concat(new string[]
									{
										"select m.MoId,MoCode,AllocateId from ",
										AddRD11.DBname,
										"..mom_order m left join ",
										AddRD11.DBname,
										"..mom_orderdetail mo on m.MoId=mo.MoId left join mom_moallocate ma on mo.MoDId=ma.MoDId where mo.MoDId='",
										Autoid,
										"' "
									}));
									moid = dtm.Rows[0]["MoId"].ToString();
									mocode = dtm.Rows[0]["MoCode"].ToString();
									modids = dtm.Rows[0]["AllocateId"].ToString();
									bool flag22 = RedVouch == "1";
									if (flag22)
									{
										decimal IssQtY = Convert.ToDecimal(SQLHelper.GetString(string.Concat(new string[]
										{
											"select sum(ISNULL(IssQtY,0))IssQty from ",
											AddRD11.DBname,
											"..mom_moallocate where MoDId in('",
											Autoid,
											"')"
										})));
										bool flag23 = IssQtY == 0m;
										if (flag23)
										{
											row = 1;
											res = res + "第[" + (i + 1).ToString() + "]行U8生产订单行还未领料不能退!";
										}
									}
								}
							}
							bool flag24 = cBusType == "委外";
							if (flag24)
							{
								DataTable dtom = SQLHelper.GetDataTable(string.Concat(new string[]
								{
									"select isnull(iMaterialSendQty,0) isendQTY from ",
									AddRD11.DBname,
									"..OM_MODetails where MODetailsID='",
									Autoid,
									"'"
								}));
								int ifex6 = Convert.ToInt32(dtom.Rows.Count);
								bool flag25 = ifex6 == 0;
								if (flag25)
								{
									row = 1;
									res = string.Concat(new string[]
									{
                                        res,
										"第[",
										(i + 1).ToString(),
										"]行U8工单明细唯一ID[",
										Autoid,
										"]在U8系统不存在!"
									});
								}
								else
								{
									DataTable dtm2 = SQLHelper.GetDataTable(string.Concat(new string[]
									{
										"select a.MOMaterialsID, b.cInvCode,c.cvencode, b.iQuantity, b.MOID, c.cCode, b.iVouchRowNo imoseq, b.iNatUnitPrice, b.citemcode, b.citemname  from ",
										AddRD11.DBname,
										"..OM_MOMaterials a (nolock)   left join ",
										AddRD11.DBname,
										"..OM_MODetails b (nolock) on a.MODetailsID=b.MODetailsID   left join ",
										AddRD11.DBname,
										"..OM_MOMain c (nolock) on b.MOID=c.MOID  where a.MODetailsID = '",
										Autoid,
										"'"
									}));
									moid = dtm2.Rows[0]["MoId"].ToString();
									omcode = dtm2.Rows[0]["cCode"].ToString();
									modids = dtm2.Rows[0]["MOMaterialsID"].ToString();
									//imoseq = dtm2.Rows[0]["imoseq"].ToString();
									cvencode= dtm2.Rows[0]["cvencode"].ToString();
									string iqty = dtm2.Rows[0]["iQuantity"].ToString();
									string icinv= dtm2.Rows[0]["cInvCode"].ToString();
									iProcessFee = AddRD11.ToDec(dtm2.Rows[0]["iNatUnitPrice"].ToString());
									bool flag26 = RedVouch == "1";
									if (flag26)
									{
										decimal isendQTY = Convert.ToDecimal(dtom.Rows[0]["isendQTY"].ToString());
										bool flag27 = isendQTY == 0;
										if (flag27)
										{
											row = 1;
											res = res + "第[" + (i + 1).ToString() + "]行U8订单行还未领料不能退!";
										}
									}
									
								}
							}
						}
						string RowNo = rd.Items[i].RowNo;
						bool flag28 = string.IsNullOrEmpty(RowNo);
						if (flag28)
						{
							row = 1;
							res = res + "第[" + (i + 1).ToString() + "]行行号不允许为空!检查RowNo字段";
						}
						string cInvCode = rd.Items[i].cInvCode;
						bool flag29 = string.IsNullOrEmpty(cInvCode);
						if (flag29)
						{
							row = 1;
							res = res + "第[" + (i + 1).ToString() + "]行存货编码不允许为空!检查cInvCode字段";
						}
						else
						{
							int ifex7 = Convert.ToInt32(SQLHelper.GetString(string.Concat(new string[]
							{
								"select COUNT(*) from ",
								AddRD11.DBname,
								"..Inventory where cinvcode='",
								cInvCode,
								"'"
							})));
							bool flag30 = ifex7 == 0;
							if (flag30)
							{
								row = 1;
								res = string.Concat(new string[]
								{
									res,
									"第[",
									(i + 1).ToString(),
									"]行存货编码[",
									cInvCode,
									"]在U8系统不存在!"
								});
							}
						}
						int iFlag = 0;
						string iQuantity = rd.Items[i].iQuantity;
						bool flag31 = string.IsNullOrEmpty(iQuantity);
						if (flag31)
						{
							row = 1;
							res = res + "第[" + (i + 1).ToString() + "]行数量不允许为空!检查iQuantity字段!";
						}
						else
						{
							bool flag32 = !PublicMet.IsNumberic(iQuantity);
							if (flag32)
							{
								row = 1;
								res = string.Concat(new string[]
								{
									res,
									"第[",
									(i + 1).ToString(),
									"]行数量[",
									iQuantity,
									"]非数字类型!"
								});
							}
                            if (!flag32)
                            {
                                if (RedVouch=="1")
                                {
									iQuantity =(Convert.ToDecimal(iQuantity) * (-1)).ToString();
									iFlag = 1;
                                }
                            }
						}
						string cBatch = rd.Items[i].cBatch;
						bool cba = Convert.ToBoolean(SQLHelper.GetString(string.Concat(new string[]
						{
							"select isnull(bInvBatch,0) from ",
							AddRD11.DBname,
							"..Inventory where cinvcode='",
							cInvCode,
							"'"
						})));
						if (cba)
						{
							bool flag34 = string.IsNullOrEmpty(cBatch);
							if (flag34)
							{
								row = 1;
								res = string.Concat(new string[]
								{
									res,
									"第[",
									(i + 1).ToString(),
									"]行存货[",
									cInvCode,
									"]进行批次管理,批次不允许为空,检查cBatch字段!"
								});
							}
						}
						string dMadeDate = rd.Items[i].dMadeDate;
						string cPosCode = rd.Items[i].cPosCode;
						bool flag35 = row == 0;
						if (flag35)
						{
							//sql = "select * from OM_MOMain where MOID='"+moid+"'";
							//DataTable dt = DBHelp.SQLHelper.GetDataTable(sql);
       //                     if ()
       //                     {

       //                     }

							bool flag36 = cBusType == "委外";
							if (flag36)
							{
								cidrd++;
								SQLHelper.ExecuteSql("update UFSystem..UA_Identity set iChildId=iChildId+1 where cAcc_Id='" + cAcc_Id + "' and cVouchType='rd' ");
								string autoid10 = "1" + string.Format("{0:D9}", cidrd);
								string cbsysbarcode = "||st10|" + dCode + "|" + (i + 1).ToString();
								sql = string.Concat(new string[]
								{
									"Insert Into ",
									AddRD11.DBname,
									"..rdrecords10(autoid,id,cinvcode,inum,iquantity,iunitcost,iprice,ipunitcost,ipprice,cbatch," +
									"cvouchcode,cinvouchcode,cinvouchtype,isoutquantity,isoutnum,cfree1,cfree2,ifnum,ifquantity,dvdate," +
									" cposition,cdefine22,cdefine23,cdefine24,cdefine25,cdefine26,cdefine27,citem_class,citemcode,cname," +
									"citemcname,cfree3,cfree4,cfree5,cfree6,cfree7,cfree8,cfree9,cfree10,cbarcode," +
									"inquantity,innum,cassunit,dmadedate,imassdate,cdefine28,cdefine29,cdefine30,cdefine31,cdefine32," +
									" cdefine33,cdefine34,cdefine35,cdefine36,cdefine37,impoids,icheckids,cbvencode,ccheckcode,icheckidbaks," +
									"crejectcode,irejectids,ccheckpersoncode,dcheckdate,cmassunit,cmolotcode,brelated,cmworkcentercode,cbaccounter,dbkeepdate," +
									" bcosting,bvmiused,ivmisettlequantity,ivmisettlenum,cvmivencode,iinvsncount,iinvexchrate,corufts,cmocode,imoseq," +
									" iopseq,copdesc,iexpiratdatecalcu,cexpirationdate,dexpirationdate,cciqbookcode,ibondedsumqty,productinids,iorderdid,iordertype," +
									"iordercode,iorderseq,isodid,isotype,csocode,isoseq,cbatchproperty1,cbatchproperty2,cbatchproperty3,cbatchproperty4," +
									" cbatchproperty5,cbatchproperty6,cbatchproperty7,cbatchproperty8,cbatchproperty9,cbatchproperty10,cbmemo,irowno,strowguid,cservicecode," +
									"cbsysbarcode,cplanlotcode,bmergecheck,imergecheckautoid,iposflag,iorderdetailid,body_outid)" +
									"Values(",autoid10,", ",ID10,", N'",cInvCode,"', Null, ",iQuantity,", Null, Null, Null, Null, ",PublicMet.setvalue(2,cBatch),"," +
									" Null, Null, Null, Null, Null, Null, Null, Null, Null, Null," +
									" Null, Null, Null, Null, Null, Null, Null, Null, Null, Null," +
									" Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, " +
									" Null, Null, Null, Null, Null, Null, Null, Null, Null, Null," +
									" Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, " +
									" Null, Null, Null, Null, Null, Null, 0, Null, Null, Null, " +
									" 1, 0, Null, Null, Null, Null, Null, Null, Null, Null," +
									" Null, Null, 0, Null, Null, Null, Null, Null, Null, 0," +
									" Null, Null, Null, 0, Null, Null, Null, Null, Null, Null," +
									" Null, Null, Null, Null, Null, Null, Null, ",
									(i + 1).ToString(),
									", Null, Null, '",cbsysbarcode,"', Null, 0, Null, Null, Null, Null)"
								});
								list.Add(sql);
								list.Add(string.Concat(new string[]
								{
									"insert into ",AddRD11.DBname,"..IA_ST_UnAccountVouch10(IDUN,IDSUN,cVouTypeUN,cBustypeUN) values ('",ID10,"','",autoid10,"','10','成品入库')"
								}));
							}
							cidrd++;
							SQLHelper.ExecuteSql("update UFSystem..UA_Identity set iChildId=iChildId+1 where cAcc_Id='" + cAcc_Id + "' and cVouchType='rd' ");
							string autoid11 = "1" + string.Format("{0:D9}", cidrd);
							string cbsysbarcode2 = "||st11|" + Code11 + "|" + (i + 1).ToString();
							string sqlunit = string.Concat(new string[]
							{
								"select  cSTComUnitCode,isnull(iChangRate,NULL) iChangRate   from ",
								AddRD11.DBname,
								"..inventory i  left join ",
								AddRD11.DBname,
								"..ComputationUnit c  on i.cSTComUnitCode=c.cComunitCode  where cinvcode='",
								cInvCode,
								"'"
							});
							DataTable dtiv = SQLHelper.GetDataTable(sqlunit);
							bool flag37 = dtiv.Rows.Count > 0;
							string cAssUnit;
							string iinvexchrate;
							if (flag37)
							{
								cAssUnit = "'" + dtiv.Rows[0]["cSTComUnitCode"].ToString() + "'";
								bool flag38 = !string.IsNullOrEmpty(dtiv.Rows[0]["iChangRate"].ToString());
								if (flag38)
								{
									iinvexchrate = (dtiv.Rows[0]["iChangRate"].ToString() ?? "");
								}
								else
								{
									iinvexchrate = "NULL";
								}
							}
							else
							{
								cAssUnit = "NULL";
								iinvexchrate = "NULL";
							}
							string iMassDate = string.Empty;
							string cMassUnit = string.Empty;
							string cinvname = string.Empty;
							DataTable imdt = AddRD11.GetMassDate(cInvCode, AddRD11.DBname);
							bool flag39 = imdt.Rows.Count > 0;
							if (flag39)
							{
								iMassDate = imdt.Rows[0]["iMassDate"].ToString();
								cMassUnit = imdt.Rows[0]["cMassUnit"].ToString();
								cinvname = imdt.Rows[0]["cinvname"].ToString();
							}
							sql = string.Concat(new string[]
							{
								"insert into ",
								AddRD11.DBname,
								"..rdrecords11(AutoID,ID,cInvCode,iQuantity,cBatch,iFlag,dVDate,cPosition," +
								" iNQuantity,dMadeDate,iMassDate,iMPoIds,cMassUnit,cmworkcentercode,bLPUseFree," +
								"iOriTrackID,bCosting,bVMIUsed,cmocode,invcode," +
								"imoseq,iopseq,copdesc,iExpiratDateCalcu,iorderdid, " +
								"iordertype,isotype,ipesodid,ipesotype,cpesocode," +
								"ipesoseq,irowno,cbsysbarcode,bcanreplace,iposflag," +
								" cItemCode,cName,cItem_class,cItemCName,cbMemo, " +
								"iProcessFee,comcode,cAssUnit,iinvexchrate,iOMoDID,iOMoMID)" +
								"values('",autoid11,"','",ID11,"','",cInvCode,"','",iQuantity,"',",PublicMet.setvalue(2,cBatch),","+iFlag+",null,NULL,'",
								iQuantity,"',null,NULL,NULL,NULL,null,0," +
								" 0,1,0,",PublicMet.setvalue(2,mocode),",",PublicMet.setvalue(2,cInvCode),"," +
								" "+PublicMet.setvalue(2,imoseq)+",null,null,0,NULL," +
								"0,0,'"+modids+"',8,"+PublicMet.setvalue(2,omcode)+"," +
								" null ,'",(i + 1).ToString(),"','",cbsysbarcode2,"',NULL,NULL," +
								" NULL,NULL,NULL,NULL,NULL,NULL,'",mocode,"'," +
								" NULL,",iinvexchrate,",'",Autoid,"','",modids,"')"
							});
							list.Add(sql);
							list.Add("update rdrecord11 set cVenCode='" + cvencode + "',iMQuantity='" + iQuantity + "',cPsPcode='" + cInvCode + "',cMPoCode='" + omcode + "',iproorderid='" + moid + "' where ID='" + ID11 + "'");
							if (cBusType == "委外")
							{
								list.Add(string.Concat(new string[]
								{
									"update ",AddRD11.DBname,"..OM_MOMaterials set iSendQTY =isnull(iSendQTY ,0)+'",iQuantity,"' where MODetailsID ='",Autoid,"'"
								}));
								sql = "update b set iMaterialSendQty=isnull(iMaterialSendQty,0)+" + iQuantity + " from " + AddRD11.DBname + "..OM_MOMaterials a (nolock) left join "+AddRD11.DBname+"..OM_MODetails b(nolock) on a.MODetailsID = b.MODetailsID  where a.MODetailsID = '" + Autoid + "'";
								list.Add(sql);
							}
							else
							{
								list.Add(string.Concat(new string[]
								{
									"update ",AddRD11.DBname,"..mom_moallocate set IssQty =isnull(IssQty ,0)+'",iQuantity,"' where MoDid ='",Autoid,"'"
								}));
							}
							list.Add(string.Concat(new string[]
							{
								"insert into ",AddRD11.DBname,"..IA_ST_UnAccountVouch11(IDUN,IDSUN,cVouTypeUN,cBustypeUN) values ('",ID11,"','",autoid11,"','11','领料')"
							}));
							mir.Autoid = Autoid;
							mir.U8RowNo = (i + 1).ToString();
							mir.U8ID = autoid11;
							lim.Add(mir);
						}
					}
					if (row == 0)
					{
						int count = SQLHelper.ExecuteSqlTran(list);
						bool flag42 = count > 0;
						if (flag42)
						{
							res += "产成品入库材料出库生单成功";
							mr.U8Code = dCode;
							mr.Items = lim;
							LogException.WriteiLog(filename, "RD11入库成功[" + dCode + "]", LogException.ilog, false);
						}
						else
						{
							row = 1;
							res += "入库失败";
						}
					}
				}
				catch (Exception ex)
				{
					row = 1;
					LogException.WriteiLog(filename, "RD11异常:" + ex.Message, LogException.ilog, true);
					res = res + "失败:" + ex.Message;
				}
			}
			else
			{
				row = 1;
				res += "未传数据!";
			}
			bool flag43 = row == 1;
			if (flag43)
			{
				mr.Code = "500";
				mr.Msg = res;
			}
			else
			{
				mr.Code = "200";
				mr.Msg = res;
			}
			return JsonConvert.SerializeObject(mr);
		}
		public static DataTable GetMassDate(string invcode, string zt)
		{
			return SQLHelper.GetDataTable(string.Concat(new string[]
			{
				"select isnull(iMassDate,0) iMassDate,cMassUnit,cinvname from ",
				zt,
				"..inventory    where cinvcode='",
				invcode,
				"' "
			}));
		}

		/// <summary>
		/// Decimal类型转换
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static decimal ToDec(string str)
		{
			decimal result;
			try
			{
				result = Convert.ToDecimal(str);
			}
			catch
			{
				result = 0m;
			}
			return result;
		}
		public static string DBname = ConfigurationManager.AppSettings["U8DBNAME"].ToString();
	}
}
